package com.jbp.service.impl;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.jbp.exception.RRException;
import com.jbp.service.PropertyService;
import com.jbp.service.SendNoteMessageService;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Set;

@Service
public class SendNoteMessageServiceZtmpl implements SendNoteMessageService {
    private static final Logger logger = LoggerFactory.getLogger(SendNoteMessageServiceZtmpl.class);
    @Autowired
    private PropertyService propertyService;
    @Override
    public boolean send(String phone, String code) {
        try {
            // userid
            String userid = "3922";

            // account
            String account = "sitake";

            // password
            String password = "2468ACDSee";

            String content = String.format("【斯塔克】您的验证码是%s，请于5分钟内正确输入", code);
            String sendTime = "";
            String action = "send";
            String extno = "";

            //追天账号
            String zt_account = propertyService.getV("msg_zt_account");
            if("youhuigou".equalsIgnoreCase(zt_account)) {
                account = "youhuigou";
                password = "2469Uyghur";
                userid = "3925";
                content = String.format("【优会购】您的验证码是%s，请于5分钟内正确输入", code);
            }

            // 服务地址
            String url = String.format("http://122.114.79.52:6688/sms.aspx");

            String urlParameters = String.format("userid=%s&account=%s&password=%s&mobile=%s&content=%s&sendTime=%s&action=%s&extno=%s",
                    userid, account, password, phone, content, sendTime, action, extno);

            String allUrl = String.format("%s?%s", url, urlParameters);
            URL obj = new URL(allUrl);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            //添加请求头
            con.setRequestMethod("GET");
            con.setDoOutput(true);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            String responseStr = response.toString();

            if (responseStr.contains("<message>ok</message>")) {
                logger.debug(String.format("给%s发送短信成功, 接口返回信息: %s", phone, responseStr));
            } else {
                logger.error(String.format("给%s发送短信失败，错误信息: %s", phone, responseStr));
                return false;
            }
        } catch (Exception ex) {
            logger.error(String.format("验证码发送失败，mobile: %s", phone), ex);
            throw new RRException("发送失败");
        }
        return true;
    }

    //public static void main(String[] args) throws Exception {
    //    SendNoteMessageServiceZtmpl http = new SendNoteMessageServiceZtmpl();
    //
    //    http.send("", "123456");
    //}
}
